<?php

include 'def.php';

header( 'content-type: application/json; charset=utf-8' );

$json_array = array( 'status'=>'error', 'error_id'=>'input', 'error_description'=>'request data error' );

session_start();

if( empty($_GET) )
{ /*do nothing*/ 
}
elseif( !isset($_GET['studentid']) || !isset($_GET['teacherid']) || !isset($_GET['classid']) )
{/*do nothing*/
}
else{
	$studentid = $_GET['studentid'];
	$teacherid = $_GET['teacherid'];
	$classid   = $_GET['classid'];
	
	/*need to push in all scripts*/
	if( !isValidMd5($studentid) || !isValidMd5($studentid) || !isValidMd5($classid) )
	{
		/*do nothing*/
	}
	else{
		$conn = mysql_connect( DBSERV, DBUSER, DBPASSWD ) or die( "Couldn't connect to database" );
		$db = mysql_select_db( DB ) or die( "Couldn't connect to database" );
		
		$query = "select rc.* from rubric rc Inner Join class cs on rc.classid=cs.classid where rc.studentid='$studentid' and rc.classid='$classid'and cs.teacherid='$teacherid';";

		$result = mysql_query( $query ) or die( 'Query_failed: ' . mysql_error() );

		/*unique id returns one row*/
		if( mysql_num_rows($result) != 1 )
		{
			$json_array = array( 'status'=>'error', 'error_id'=>'$id','error_description'=>'data no found' );
		}
		else
		{
			$row = mysql_fetch_assoc( $result );
			
			$rubric_data = array( 'studentid'=>sprintf('<%s>', $row['studentID']), 'type'=>sprintf('<%s>',$row['type']) );
			
			$arr = fetchRows( array('rubricgradeline', 'rubricgradelevel', 'rubricSection' ), 'rubricid', $row['id'] );			
			$rubric_data = array_merge( $rubric_data, $arr );
	
			$json_array = array( 'status'=>'OK', 'date'=>sprintf( '<%d>', strtotime($row['lastupdate'])) );
			
			$json_array['data'] = sprintf('<%s>', json_encode($rubric_data));
			
		}
		
		}
	}
		
echo( json_encode($json_array) );

?>